<template>
	<view class="content">
		<view class="head">
			<view class="logo">
				<!-- logo -->
				<image style="width: 100rpx;height: 100rpx;" src="../../static/account.png" mode=""></image>
			</view>
			<text class="tip">欢迎登录超市库存管理系统</text>
		</view>
		<view class="form-box">
			<view class="tab-box">
				<view class="row-tab" @click="tabClick(0)">
					<text :class="tabIndex==0?'tab-active':''">员工登录</text>
					<text class="thing" v-show="tabIndex==0?true:false"></text>
				</view>
				<view class="row-tab" @click="tabClick(1)">
					<text :class="tabIndex==1?'tab-active':''">管理员登录</text>
					<text class="thing" v-show="tabIndex==1?true:false"></text>
				</view>
			</view>
			<view>
				<view class="row-input">
					<input maxlength="11" v-model="account" placeholder="账号" />
				</view>
				<view class="row-input">
					<input password v-model="password" maxlength="18" placeholder="密码" />
				</view>
			</view>
			<view class="login-btn" @click="login">
				登录
			</view>
			<view class="menu-link" style="margin-left: 32%;">
				<text @click="title">忘记密码</text>
			</view>
		</view>
	</view>
</template>

<script>
	import changstate from '../../until/chanstate.js'
	export default {
		data() {
			return {
				tabIndex:'',show:true,src:require('../../static/paimg/可见.png'),
				value:0,account:'111111',password:'123456'
			}
		},
		onLoad() {
			uni.setStorageSync('account','')
			// 从本地缓存中同步获取指定 key 对应的内容，用于判断是否是第一次打开应用
		},
		methods: {
			// 是否选择
			tabClick(itype){
				this.tabIndex = itype
			},
			async login(){
				var account = this.account;
				var password = this.password;
				var user = this.tabIndex;
				// account.replace(/\s*/g,"")去除字符串内所有的空格
				if(account.replace(/\s*/g,"").length == 0){
					uni.showToast({
						icon:'error',
						position: 'bottom',
						title: '账号不能为空'
					})
				}else if(password.replace(/\s*/g,"").length ==0){
					uni.showToast({
						icon:'error',
						position: 'bottom',
						title: '密码不能为空'
					})
				}else{
					if(user==0){
						var data2 = [];
						const resac = await this.Request({
							url:'api/login/login_em_ac',
							method:'GET',
							data:{account:account}
						})
						for(let i= 0;i<resac.data.length;i++) {
						     data2[i] = resac.data[i].account;
						}
						if(data2.includes(account)){
							const res = await this.Request({
								url:'api/login/login_em_ps',
								method:'POST',
								data:{account:account,password:password}
							})
							uni.setStorageSync('token', res.data.token);
							var user = res.data.deid[0].departmentid
							if(res.statusCode!=404){
								uni.showToast({
									title: '登录成功'
								})
								if(user==1){
									uni.switchTab({
										url:'/pages/buyer/buy-commodity/buy-commodity'
									})
									changstate.changstate()
									uni.setStorageSync('account',this.account)
								}else if(user==2){
									uni.switchTab({
										url:'/pages/businessr/bus-commodity/bus-commodity'
									})
									changstate.changstate()
									uni.setStorageSync('account',this.account)
								}
								else if(user==3){
									uni.switchTab({
										url:'/pages/warekeeper/war-commodity/war-commodity?'
									})
									changstate.changstate()
									uni.setStorageSync('account',this.account)
								}
							}else{
								uni.showToast({
									icon:'error',
									position: 'bottom',
									title: '密码错误'
								})
								this.password="";	
							}
						}else{
							uni.showToast({
								title:"账号不存在，请重试!"
							})
							this.account="";
							this.password="";
						}
					}else if(user==1){
						var data1 = [];
						const resac = await this.Request({
							url:'api/login/login_sp_ac',
							method:'GET',
							data:{account:account}
						})
						for(let i= 0;i<resac.data.length;i++) {
						     data1[i] = resac.data[i].account;
						}
						if(data1.includes(account)){
							const res = await this.Request({
								url:'api/login/login_sp_ps',
								method:'POST',
								data:{account:account,password:password}
							})
							uni.setStorageSync('token', res.data.token);
							if(res.statusCode!=404){
								uni.showToast({
									title: '登录成功'
								})
								uni.switchTab({
									url:'/pages/super/sup-information/sup-information'
								})
								changstate.changstate()
								uni.setStorageSync('account',this.account)
							}else{
								uni.showToast({
									title:"密码错误",
									icon:"error"
								})
								this.password="";
							}
						}else{
								uni.showToast({
									title:"账号不存在，请重试!"
								})
								this.account="";
								this.password="";
						}
					}	
				}
			},
			title(){
				uni.showToast({
					icon:'none',
					title:'请联系管理员找回密码'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #007AFF;
	}

	.head {
		padding-left: 80rpx;
		height: 30vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-start;

		.logo {
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 8rpx;
			width: 110rpx;
			height: 110rpx;
			font-size: 40rpx;
			color: #007AFF;
			font-weight: bold;
			background-color: #FFFFFF;
		}

		.tip {
			padding-top: 50rpx;
			color: #FFFFFF;
			font-size: 40rpx;
			letter-spacing: 5rpx;
			font-weight: bold;
		}
	}

	.form-box {
		padding: 0rpx 80rpx;
		height: 70vh;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		background-color: #FFFFFF;

		.tab-box {
			padding-top: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 80rpx;
			color: #929293;
			font-weight: bold;

			.row-tab {
				display: flex;
				flex-direction: column;
				justify-content: flex-start;
				align-items: center;
				font-weight: bold;
				font-size: 35rpx;
				width: 50%;
				height: 100%;

				.tab-active {
					color: #4d88f8;
				}

				.thing {
					margin-top: 20rpx;
					width: 50rpx;
					height: 6rpx;
					background-color: #3884f6;
				}
			}
		}

		.row-input {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-top: 50rpx;
			font-size: 30rp;
			height: 100rpx;
			border-bottom: 1rpx solid #cfcfd0;

			input {
				flex: 1;
			}

			.code {
				display: flex;
				justify-content: flex-end;
				font-weight: bold;
				color: #0b77f7;
				width: 200rpx;
				font-size: 26rpx;
			}
		}

		.login-btn {
			margin-top: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100rpx;
			letter-spacing: 10rpx;
			background: linear-gradient(to right, #0a7df7, #0f62f3);
			color: #FFFFFF;
			border-radius: 10rpx;
			font-size: 30rpx;
		}

		.menu-link {
			position: fixed;
			left: 80rpx;
			right: 80rpx;
			bottom: 50rpx;
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			font-weight: bold;
		}
	}
</style>

